package com.me.jnrgame;

import media.AudioManager;
import media.Musics;
import media.Score;
import media.Sounds;
import java.util.ArrayList;
import java.util.HashMap;

import javax.activity.ActivityCompletedException;

import objects.AbstractObject;
import objects.ItemBlock;
import screens.LevelScreen;
import screens.MainMenuScreen;
import screens.OptionsScreen;
import screens.PauseMenuWindow;
import screens.SplashScreen;
import screens.StartGameScreen;
import world.HighScoreSystem;
import world.WorldManager;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;

public class JnR extends Game implements ApplicationListener {

	private OrthographicCamera camera;
	private float screenWidth;
	private float screenHeight;
	private WorldManager worldAssistant;
	private boolean gamePaused;
	private MainMenuScreen mainMenuScreen;
	private StartGameScreen startGameScreen;
	private OptionsScreen optionsScreen;
	private LevelScreen levelScreen;
	private PauseMenuWindow pauseMenu;
	private HighScoreSystem highScoreSystem;

	/**
	 * The libgdx SpriteBatch -- used to optimize sprite drawing.
	 */

	@Override
	public void create() {
		AudioManager.loadAudio();

		Gdx.input.setCatchBackKey(true);
		gamePaused = false;
		screenWidth = 800;
		screenHeight = 480;
		// camera = new OrthographicCamera(Gdx.graphics.getWidth(),
		// Gdx.graphics.getHeight());
		camera = new OrthographicCamera(screenWidth, screenHeight);
		highScoreSystem = new HighScoreSystem();
		setScreen(new SplashScreen(this));
	}

	@Override
	public void dispose() {
		super.dispose();
		Gdx.app.exit();
	}

	@Override
	public void render() {
		super.render();
		if (worldAssistant != null && levelScreen != null && !gamePaused
				&& this.getScreen().equals(levelScreen)) {
			worldAssistant.renderWorld();

		} else {
			if (gamePaused) {
				worldAssistant.renderPauseMenu();
			}
		}
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);

	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	public void setUpLevel(LevelScreen screen) {
		if (levelScreen != null)
			levelScreen.dispose();
		
		System.gc();
		System.runFinalization();
		System.gc();
		
		levelScreen = screen;

		this.setScreen(levelScreen);
		worldAssistant = levelScreen.getWorldAssitant();

	}

	public OrthographicCamera getCamera() {
		return camera;

	}

	public float getScreenWidth() {
		return screenWidth;
	}

	public float getScreenHeight() {

		return screenHeight;
	}

	public void setMainMenuScreen(MainMenuScreen screen) {
		mainMenuScreen = screen;
	}

	public void setStartGameScreen(StartGameScreen screen) {
		startGameScreen = screen;
	}

	public void setOptionsScreen(OptionsScreen screen) {
		optionsScreen = screen;

	}

	public MainMenuScreen getMainMenuScreen() {
		return mainMenuScreen;
	}

	public StartGameScreen getStartGameScreen() {
		return startGameScreen;
	}

	public OptionsScreen getOptionsScreen() {

		return optionsScreen;
	}

	public LevelScreen getLevelScreen() {
		return levelScreen;
	}

	public void resetViewPort() {
		camera.viewportWidth = screenWidth;
		camera.viewportHeight = screenHeight;
		camera.update();

	}

	public void addHighScore(String name, float time, int levelNumber) {

		highScoreSystem.addHighScore(new Score(name, time));
	}

	public String getHighScore(int i) {

		return highScoreSystem.getScoreString(i);
	}

	public void pauseGame(boolean pause) {
		if (pause == true) {

			worldAssistant.pauseStopWatch();
		} else {
			worldAssistant.resumeStopWatch();
		}
		gamePaused = pause;
	}

	public void setLevelScreen(LevelScreen screen) {
		levelScreen = screen;
	}

	public void setPauseMenu(PauseMenuWindow _pauseMenu) {
		pauseMenu = _pauseMenu;

	}

	public void addPauseMenu() {

		if (pauseMenu != null) {

			worldAssistant.getStage().addActor(pauseMenu);
		}
	}

	public boolean isGamePaused() {
		return gamePaused;
	}

	public void toMainMenuScreen() {
		if (mainMenuScreen == null) {
			mainMenuScreen = new MainMenuScreen(this);
		}
		Gdx.input.setInputProcessor(mainMenuScreen.getStage());

		setScreen(mainMenuScreen);
		AudioManager.playMusic(Musics.Theme);

	}

	public void toOptionsScreen(int parentID) {
		if (optionsScreen == null) {
			optionsScreen = new OptionsScreen(this);
		}
		optionsScreen.setParentID(parentID);
		Gdx.input.setInputProcessor(optionsScreen.getStage());
		setScreen(optionsScreen);

	}

	public void toStartGameScreen() {
		if (startGameScreen == null) {
			startGameScreen = new StartGameScreen(this);
		}
		Gdx.input.setInputProcessor(startGameScreen.getStage());

		setScreen(startGameScreen);

	}

}
